import { defineStore } from 'pinia';

export enum ThemeType {
  WEDDING = 'wedding',      // 婚礼主题
  FUNERAL = 'funeral',      // 白事主题
  BIRTHDAY = 'birthday',    // 生日主题
  GRADUATION = 'graduation',// 升学主题
  BUSINESS = 'business',    // 商务主题
  HOUSEWARMING = 'housewarming', // 乔迁主题
  FULLMOON = 'fullmoon',    // 满月主题
  OTHER = 'other'           // 其他主题
}

// 主题颜色配置
const themeColors = {
  [ThemeType.WEDDING]: {
    backgroundColor: '#ff6b6b',
    frontColor: '#ffffff',
    primaryColor: '#FF4D4F',
    primaryDark: '#FF1F1F',
    primaryLight: '#FFE5E5',
    textPrimary: '#333333',
    textSecondary: '#666666',
    borderColor: '#EEEEEE',
    buttonColor: '#FF4D4F',
    buttonTextColor: '#FFFFFF'
  },
  [ThemeType.FUNERAL]: {
    backgroundColor: '#2c3e50',
    frontColor: '#ffffff',
    primaryColor: '#2c3e50',
    primaryDark: '#1a2634',
    primaryLight: '#3c546c',
    textPrimary: '#333333',
    textSecondary: '#666666',
    borderColor: '#EEEEEE',
    buttonColor: '#2c3e50',
    buttonTextColor: '#FFFFFF'
  },
  [ThemeType.BIRTHDAY]: {
    backgroundColor: '#FF4D4F',
    frontColor: '#ffffff',
    primaryColor: '#FF4D4F',
    primaryDark: '#FF1F1F',
    primaryLight: '#FFE5E5',
    textPrimary: '#333333',
    textSecondary: '#666666',
    borderColor: '#EEEEEE',
    buttonColor: '#FF4D4F',
    buttonTextColor: '#FFFFFF'
  },
  [ThemeType.GRADUATION]: {
    backgroundColor: '#1890FF',
    frontColor: '#ffffff',
    primaryColor: '#1890FF',
    primaryDark: '#096DD9',
    primaryLight: '#E6F7FF',
    textPrimary: '#333333',
    textSecondary: '#666666',
    borderColor: '#EEEEEE',
    buttonColor: '#1890FF',
    buttonTextColor: '#FFFFFF'
  },
  [ThemeType.BUSINESS]: {
    backgroundColor: '#722ED1',
    frontColor: '#ffffff',
    primaryColor: '#722ED1',
    primaryDark: '#531DAB',
    primaryLight: '#F9F0FF',
    textPrimary: '#333333',
    textSecondary: '#666666',
    borderColor: '#EEEEEE',
    buttonColor: '#722ED1',
    buttonTextColor: '#FFFFFF'
  },
  [ThemeType.HOUSEWARMING]: {
    backgroundColor: '#52C41A',
    frontColor: '#ffffff',
    primaryColor: '#52C41A',
    primaryDark: '#389E0D',
    primaryLight: '#F6FFED',
    textPrimary: '#333333',
    textSecondary: '#666666',
    borderColor: '#EEEEEE',
    buttonColor: '#52C41A',
    buttonTextColor: '#FFFFFF'
  },
  [ThemeType.FULLMOON]: {
    backgroundColor: '#F759AB',
    frontColor: '#ffffff',
    primaryColor: '#F759AB',
    primaryDark: '#EB2F96',
    primaryLight: '#FFF0F6',
    textPrimary: '#333333',
    textSecondary: '#666666',
    borderColor: '#EEEEEE',
    buttonColor: '#F759AB',
    buttonTextColor: '#FFFFFF'
  },
  [ThemeType.OTHER]: {
    backgroundColor: '#13C2C2',
    frontColor: '#ffffff',
    primaryColor: '#13C2C2',
    primaryDark: '#08979C',
    primaryLight: '#E6FFFB',
    textPrimary: '#333333',
    textSecondary: '#666666',
    borderColor: '#EEEEEE',
    buttonColor: '#13C2C2',
    buttonTextColor: '#FFFFFF'
  }
};

// tabBar 图标配置的接口定义
interface TabBarIcon {
  normal: string;
  active: string;
}

interface TabBarIcons {
  home: TabBarIcon;
  giftbook: TabBarIcon;
  contact: TabBarIcon;
  statistics: TabBarIcon;
  settings: TabBarIcon;
}

type ThemeTabBarIcons = Record<ThemeType, TabBarIcons>;

// tabBar 图标配置
const baseTabBarIcons: Partial<ThemeTabBarIcons> = {
  [ThemeType.WEDDING]: {
    home: {
      normal: '/static/tabbar/home.png',
      active: '/static/tabbar/home-active-sad.png'
    },
    giftbook: {
      normal: '/static/tabbar/giftbook.png',
      active: '/static/tabbar/giftbook-active-sad.png'
    },
    contact: {
      normal: '/static/tabbar/contact.png',
      active: '/static/tabbar/contact-active-sad.png'
    },
    statistics: {
      normal: '/static/tabbar/stats.png',
      active: '/static/tabbar/stats-active-sad.png'
    },
    settings: {
      normal: '/static/tabbar/settings.png',
      active: '/static/tabbar/settings-active-sad.png'
    }
  },
  [ThemeType.FUNERAL]: {
    home: {
      normal: '/static/tabbar/home.png',
      active: '/static/tabbar/home-active-sad.png'
    },
    giftbook: {
      normal: '/static/tabbar/giftbook.png',
      active: '/static/tabbar/giftbook-active-sad.png'
    },
    contact: {
      normal: '/static/tabbar/contact.png',
      active: '/static/tabbar/contact-active-sad.png'
    },
    statistics: {
      normal: '/static/tabbar/stats.png',
      active: '/static/tabbar/stats-active-sad.png'
    },
    settings: {
      normal: '/static/tabbar/settings.png',
      active: '/static/tabbar/settings-active-sad.png'
    }
  }
};

// 为其他主题复制婚礼主题的图标
const tabBarIcons = { ...baseTabBarIcons } as ThemeTabBarIcons;
const weddingIcons = tabBarIcons[ThemeType.WEDDING];

[
  ThemeType.BIRTHDAY,
  ThemeType.GRADUATION,
  ThemeType.BUSINESS,
  ThemeType.HOUSEWARMING,
  ThemeType.FULLMOON,
  ThemeType.OTHER
].forEach(theme => {
  tabBarIcons[theme] = weddingIcons;
});

// 路由映射接口
type TabRoutes = Record<string, number>;

interface ThemeState {
  currentTheme: ThemeType;
}

export const useThemeStore = defineStore('theme', {
  state: (): ThemeState => ({
    currentTheme: ThemeType.WEDDING
  }),
  
  actions: {
    // 初始化主题
    async initTheme() {
      try {
        const savedTheme = uni.getStorageSync('theme');
        if (savedTheme && Object.values(ThemeType).includes(savedTheme)) {
          this.setTheme(savedTheme as ThemeType);
        }
      } catch (error) {
        console.error('初始化主题失败:', error);
      }
    },

    // 设置主题
    setTheme(theme: ThemeType) {
      this.currentTheme = theme;
      try {
        // 保存主题设置
        uni.setStorageSync('theme', theme);
        
        // 设置导航栏颜色
        uni.setNavigationBarColor({
          frontColor: '#ffffff',
          backgroundColor: themeColors[theme].backgroundColor,
          animation: {
            duration: 0,
            timingFunc: 'linear'
          }
        });

        // 只在需要时更新 tabBar 相关设置
        const pages = getCurrentPages();
        const currentPage = pages[pages.length - 1];
        const isTabBarPage = currentPage && this.getTabIndexByRoute(currentPage.route) >= 0;

        // 只在 tabBar 页面更新
        if (isTabBarPage) {
          // 设置底部标签栏样式
          uni.setTabBarStyle({
            color: '#999999',
            selectedColor: themeColors[theme].primaryColor,
            backgroundColor: '#ffffff',
            borderStyle: 'black'
          });

          // 更新所有 tabBar 图标
          const icons = tabBarIcons[theme] || tabBarIcons[ThemeType.WEDDING];
          const tabItems = ['home', 'giftbook', 'contact', 'statistics', 'settings'] as const;
          tabItems.forEach((key, index) => {
            uni.setTabBarItem({
              index,
              iconPath: icons[key].normal,
              selectedIconPath: icons[key].active
            });
          });
        }

        // 强制更新当前页面
        if (currentPage?.$vm) {
          currentPage.$vm.$forceUpdate();
        }
      } catch (error) {
        console.error('主题设置失败:', error);
        // 显示错误提示
        uni.showToast({
          title: '主题切换失败',
          icon: 'none'
        });
      }
    },

    // 根据路由获取 tabBar 索引
    getTabIndexByRoute(route: string): number {
      const tabRoutes: TabRoutes = {
        'pages/index/index': 0,
        'pages/giftbook/index': 1,
        'pages/contact/index': 2,
        'pages/statistics/index': 3,
        'pages/settings/index': 4
      };
      return route in tabRoutes ? tabRoutes[route] : -1;
    }
  },

  persist: true
}); 